import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Req,
  Delete,
} from '@nestjs/common';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dto/create-article.dto';
import { UpdateArticleDto } from './dto/update-article.dto';
import { Public } from 'src/auth/utils/public.metadata';
import { LogAction } from 'src/auth/utils/logging.metadata';

@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  @LogAction('创建文章')
  @Post('create')
  createArticle(
    @Body() createArticleDto: CreateArticleDto,
    @Req() req: AuthRequest,
  ) {
    return this.articleService.create(createArticleDto, req.user.userId);
  }

  @Public()
  @Get('list')
  findAllArticle() {
    return this.articleService.findAll();
  }

  // @Public()
  @Get(':id')
  findOne(@Param('id') id: string, @Req() req: AuthRequest) {
    return this.articleService.findOne(+id, req?.user?.userId);
  }

  @LogAction('修改文章')
  @Post('update/:id')
  update(@Param('id') id: string, @Body() updateArticleDto: UpdateArticleDto) {
    return this.articleService.update(+id, updateArticleDto);
  }

  @Delete('delete/:id')
  remove(@Param('id') id: string) {
    return this.articleService.remove(+id);
  }
}
